//
//  HZL_TravelNotesDetailPageViewController.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/28.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_TravelNotesDetailPageViewController.h"
#import "HZL_travelNotesDetailPageTableViewCell.h"
#import "HZL_travelNotesPageUserTableViewCell.h"
#import "HZL_MapViewController.h"
#import "HZL_TravelNotesDetailLocationPageViewController.h"
//#import "CXY_MapViewController.h"
@interface HZL_TravelNotesDetailPageViewController ()<UITableViewDataSource, UITableViewDelegate, jumpToLocationPage>
@property (nonatomic, retain)UICollectionView *collectionView;

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)HZL_RecommendModel *noteModel; //包含所有信息的model



@property (nonatomic, retain)HZL_RecommendModel *userModel;
@property (nonatomic, retain)NSMutableArray *modelDayArr; //保存游记具体内容总数据
@property (nonatomic, retain)NSMutableArray *modelWaypointsArr; //保存每天waypoints套对象的数组
@property (nonatomic, retain)NSMutableArray *modelLocationArr;  //保存每天的location对象


@end

@implementation HZL_TravelNotesDetailPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     [self addScreenEdgePan];
    
    [self getData];
    [self createTableView];
    [self loadMBProgress];
}
- (void)getData
{
    NSString *url = nil;
    url = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/%ld", (long)self.travelNotesModel.id.integerValue];

    [HZL_NetworkingHandle getUrl:url cookie:nil result:^(id result) {
        NSMutableDictionary *tempDic = result;
        self.noteModel = [[HZL_RecommendModel alloc]init];
        [self.noteModel setValuesForKeysWithDictionary:tempDic]; //把当前字典转化成对象  包含所有信息
        
        self.userModel = [[HZL_RecommendModel alloc]init];
        [self.userModel setValuesForKeysWithDictionary:result[@"user"]]; //用户信息model
        
        self.modelDayArr = [HZL_RecommendModel transformWithArray:result[@"days"]];  //保存游记具体内容总数据
        
        NSMutableArray *waypointsArr = [NSMutableArray array]; //保存所有waypoints数组
        self.modelLocationArr = [NSMutableArray array];  //保存所有location的 model对象
        
        for (NSDictionary *dic in result[@"days"]) {
            
            NSMutableArray *tempWaypointArr = [NSMutableArray array];
            for (NSMutableDictionary *dic2 in dic[@"waypoints"]) {
                if (dic2[@"id"] != nil) //有的waypoints字典里并没有数据
                {
                    [tempWaypointArr addObject:dic2];
                }
                
            }
            [waypointsArr addObject:tempWaypointArr]; //数组套数组套waypoints对象
            NSMutableArray *tempArr = [NSMutableArray array];  //用来保存每天的waypoint里的location对象
            //遍历每天得waypoint数组
            for (NSMutableDictionary *dic in tempWaypointArr)
            {
                if ([dic[@"location"] class] == [NSNull class]) {
                    HZL_RecommendModel *model = [[HZL_RecommendModel alloc]init];
                    [tempArr addObject:model];
                }else{
                    //保存location对象
                    HZL_RecommendModel *model = [[HZL_RecommendModel alloc]init];
                    [model setValuesForKeysWithDictionary: dic[@"location"]];
                    [tempArr addObject:model];
                }
                
            }
            
            [self.modelLocationArr addObject:tempArr];
            
        }
        
        self.modelWaypointsArr = [NSMutableArray array];
        for (NSMutableArray *arr in waypointsArr) {
            [self.modelWaypointsArr addObject: [HZL_RecommendModel transformWithArray:arr]];
        }
        
        [self.hud removeFromSuperview];
        [self.tableView reloadData];
        
    }];
    
}



#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // self.tableView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    [self.view addSubview:self.tableView];
    
    
}
#pragma mark - 游记详情 location跳转下一页 button协议方法
- (void)jumpToLocation:(NSIndexPath *)indexpath
{
    if ([self.modelLocationArr[indexpath.section - 1][indexpath.row] longitude] && ![self.modelWaypointsArr[indexpath.section - 1][indexpath.row]poiName]) {
        
        
    }
    else
    {
        HZL_TravelNotesDetailLocationPageViewController *pageView = [[HZL_TravelNotesDetailLocationPageViewController alloc]init];
        pageView.waypointsModel = self.modelWaypointsArr[indexpath.section - 1][indexpath.row];
        
        [self.navigationController pushViewController:pageView animated:YES];

    }
    
    
}


#pragma mark - tableView分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.modelWaypointsArr.count + 1;
}

#pragma mark - tableView分区头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else{
        return 20;
    }
}
#pragma mark - tableView头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }
    else{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 20)];
        view.backgroundColor  = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        
        UIView *viewCircle = [[UIView alloc]initWithFrame:CGRectMake(20, -20, 20, 20)];
        viewCircle.layer.masksToBounds = YES;
        viewCircle.layer.cornerRadius = 10;
        viewCircle.backgroundColor = [UIColor colorWithRed:0.89 green:0.87 blue:0.78 alpha:1];
        [view addSubview:viewCircle];
        
        
        UIView *viewLine = [[UIView alloc]initWithFrame:CGRectMake(28, viewCircle.frame.size.height + viewCircle.frame.origin.y, 5, 20)];
        viewLine.backgroundColor = [UIColor colorWithRed:0.89 green:0.87 blue:0.78 alpha:1];
        [view addSubview:viewLine];
        
        
        UILabel *labelDay = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 50, 12)];
        labelDay.textColor = [UIColor colorWithRed:0.79 green:0.73 blue:0.6 alpha:1];
        labelDay.font = [UIFont systemFontOfSize:15];
        [view addSubview:labelDay];
        
        
        UILabel *labelDate = [[UILabel alloc]initWithFrame:CGRectMake(110, 0, 150, 12)];
        labelDate.textColor = [UIColor colorWithRed:0.79 green:0.73 blue:0.6 alpha:1];
        labelDate.font = [UIFont systemFontOfSize:15];
        [view addSubview:labelDate];
       
        
        NSNumber *day =  ((HZL_RecommendModel *) self.modelDayArr[section - 1]).day;
        labelDay.text = [NSString stringWithFormat:@"%@%@%@", @"第", day, @"天"];
        
        NSString *date = ((HZL_RecommendModel *) self.modelDayArr[section - 1]).date;
        labelDate.text = [date substringFromIndex:5];
        
        //夜间模式
        if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
            view.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
            labelDay.textColor = [UIColor whiteColor];
            labelDate.textColor = [UIColor whiteColor];
        }else
        {
            self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        }
        
        
        
        return view;
    }
}

#pragma mark - tableView行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    else
    {
        return [self.modelWaypointsArr[section - 1] count];
    }
    
}
#pragma mark - tableView Cell赋值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        HZL_travelNotesPageUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"user"];
        if (!cell) {
            cell = [[HZL_travelNotesPageUserTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"user"];
        }
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        cell.modelUser = self.userModel;
        cell.modelNotes = self.noteModel;
        
        return cell;
        
        
    }
    else{
        
        
        HZL_travelNotesDetailPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"note"];
        if (!cell) {
            cell = [[HZL_travelNotesDetailPageTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"note"];
        }
        
        
        //向cell传递indexpath 因为数据在当前页 协议点击方法需要数据的位置  又取不到section  和 row  所以需要传递进去  而不能在当前页传递是因为一次可能会出现多个cell  数据不准
        cell.indexpath = indexPath;
        
        //        //判断是是否有跳到地点详情的url是否有type
        //        cell.type = self.modelWaypointsArr
        
        cell.delegate = self; // location按钮代理人为当前controller
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
        cell.modelTravelNotes = self.noteModel;
        cell.modelUser = self.userModel;
        cell.modelWaypoint = self.modelWaypointsArr[indexPath.section - 1][indexPath.row];
        return cell;
    }
}



#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 60 + (self.view.frame.size.width - 20) / 8 + 10 + 10 + 30 + 50 + 20;
    }
    else{
        HZL_RecommendModel *tempModel = self.modelWaypointsArr[indexPath.section - 1][indexPath.row];
        CGFloat imgH = (tempModel.h).integerValue;
        CGFloat imgW = (tempModel.w).integerValue;
        
        CGFloat h = [[self class]heightWithText:tempModel.text fontSize:15 Width:VIEW_WIDTH - 40];
        // imgH * WIDTH / imgW + 10 + h + 10 + 15 + 10 白色背景图高度
        if (imgH!=0 && imgW != 0) {
            return imgH * VIEW_WIDTH / imgW + 10 + h + 10 + 15 + 10 + 20;
            
        }
        else
        {
            
            return 10 + h + 10 + 15 + 10 + 20;
        }
        
    }
}
#pragma mark - tableView选中方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
}

#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.tableView addGestureRecognizer:screenEdgePan];
}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
